package net.pws.oos.biz.service.storage;

import java.io.File;

import net.pws.common.util.ImageUtils;
import net.pws.common.util.IoUtil;

import org.springframework.web.multipart.MultipartFile;

public class PhotoStorage implements FileStorage {
	
	public static final String DEFAULT_EXT_NAME = ".jpg";
	
	private String[] validExtensions = {"jpg", "png", "gif"};
    
    private MultipartFile content;
    
    private String sourcePath;
    
    private String mediumPath;
    
    private String smallPath;
    
    public PhotoStorage(MultipartFile content,
                        String sourcePath,
                        String mediumPath,
                        String smallPath) {
        this.content = content;
        this.sourcePath = sourcePath;
        this.mediumPath = mediumPath;
        this.smallPath = smallPath;
    }
    
    public String[] getValidExtensions() {
        return validExtensions;
    }
    
    public void store() throws Exception {
        // 检查路径
        String sourceFolder = IoUtil.getFolder(sourcePath);
        new File(sourceFolder).mkdirs();
        
        // 保存源图片
        File sourcePhoto = new File(sourcePath);
        content.transferTo(sourcePhoto);
        
        // 创建中等缩略图
        ImageUtils.createSnapshot(sourcePhoto, mediumPath, 600, 400, true);
        
        // 创建小型缩略图
        ImageUtils.createSnapshot(sourcePhoto, smallPath, 300, 200, true);
    }
    
}
